package com.lsxh.maoqiuqiu.dao.impl;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.lsxh.maoqiuqiu.dao.IRUserDao;
import com.lsxh.maoqiuqiu.db.DatabasePool;
import com.lsxh.maoqiuqiu.modul.RUser;

public class RUserDaoImpl extends BaseDao implements IRUserDao {
	Logger logger = Logger.getLogger(RUserDaoImpl.class);

	@Override
	public RUser getById(int uid) {
		// TODO Auto-generated method stub
		RUser result = null;
		sql = "select * from ruser where userId = ?;";
		try {
			conn = DatabasePool.getConnection();
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, uid);
			rs = psmt.executeQuery();
			if (rs.next()) {
				result = new RUser();
				result.setUserId(rs.getLong("userId"));
				result.setUsername(rs.getString("username"));
				result.setAccesstoken(rs.getString("accesstoken"));
				result.setLoginno(rs.getInt("loginno"));
				result.setBrodno(rs.getInt("brodno"));
				result.setCredit(rs.getInt("credit"));
				result.setProId(rs.getLong("proid"));
				result.setLastTime(rs.getTimestamp("lastTime"));
			}
		} catch (SQLException e) {
			logger.error("queue error: " + e.getLocalizedMessage());
			return null;
		} finally {
			close();
		}
		return result;
	}

	@Override
	public List<RUser> getUserList() {
		// TODO Auto-generated method stub

		List<RUser> resultList = new ArrayList<RUser>();
		RUser result = null;
		sql = "select * from ruser order by userid;";
		try {
			conn = DatabasePool.getConnection();
			psmt = conn.prepareStatement(sql);
			rs = psmt.executeQuery();
			while (rs.next()) {
				result = new RUser();
				result.setUserId(rs.getLong("userId"));
				result.setUsername(rs.getString("username"));
				result.setAccesstoken(rs.getString("accesstoken"));
				result.setLoginno(rs.getInt("loginno"));
				result.setBrodno(rs.getInt("brodno"));
				result.setCredit(rs.getInt("credit"));
				result.setProId(rs.getLong("proid"));
				result.setLastTime(rs.getTimestamp("lastTime"));
				resultList.add(result);
			}
		} catch (SQLException e) {
			logger.error("queue error: " + e.getLocalizedMessage());
			return null;
		} finally {
			close();
		}
		return resultList;
	}

	@Override
	public boolean register(RUser user) {
		// TODO Auto-generated method stub
		try {
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			conn = DatabasePool.getConnection();
			sql = "insert into ruser(userId,username,accesstoken,loginno,brodno,credit,proid,lastTime)"
					+ " values(?,?,?,?,?,?,?,?)";
			psmt = conn.prepareStatement(sql);
			psmt.setLong(1, user.getUserId());
			psmt.setString(2, user.getUsername());
			psmt.setString(3, user.getAccesstoken());
			psmt.setInt(4, user.getLoginno());
			psmt.setInt(5, user.getBrodno());
			psmt.setInt(6, user.getCredit());
			psmt.setLong(7, user.getProId());
			psmt.setTimestamp(8, timestamp);
			psmt.execute();
		} catch (SQLException e) {
			logger.error("insert error: " + e.getLocalizedMessage());
			return false;
		} finally {
			close();
		}
		return true;
	}

	@Override
	public boolean update(RUser user) {
		// TODO Auto-generated method stub

		try {
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			conn = DatabasePool.getConnection();
			sql = "update ruser set username = ?, accesstoken = ?, loginno = ?, brodno = ?, credit = ?, proid = ?, lastTime = ? where userId = ?;";
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, user.getUsername());
			psmt.setString(2, user.getAccesstoken());
			psmt.setInt(3, user.getLoginno());
			psmt.setInt(4, user.getBrodno());
			psmt.setInt(5, user.getCredit());
			psmt.setLong(6, user.getProId());
			psmt.setTimestamp(7, timestamp);
			psmt.setLong(8, user.getUserId());
			psmt.executeUpdate();
		} catch (SQLException e) {
			logger.error("update error: " + sql + e.getLocalizedMessage());
		} finally {
			close();
		}
		return true;
	}

	public static void main(String[] args) {
		RUser user = new RUser();
		user.setUsername("keller11111");
		user.setLoginno(3);
		RUserDaoImpl dao = new RUserDaoImpl();
		dao.register(user);
	}
}
